package dasregistry.utils;

import java.util.List;
import java.util.StringTokenizer;

import javax.servlet.http.HttpServletRequest;

/**
 * class to hold methods for filtering rows based on rows
 * @author jw12
 *
 */
public class RowsFilter {
	int start = 0;
	int stop = 0;
	int total=0;//stores total before rows filtering
	public int getStart() {
		return start;
	}

	public int getStop() {
		return stop;
	}

	
	public List filterByRows(List list, HttpServletRequest req){
	
		this.total=list.size();
		String rows = req.getParameter("rows");
		if (rows != null) {
			StringTokenizer token = new StringTokenizer(rows, "-");
			String startString = token.nextToken();
			String stopString = token.nextToken();
		//System.out.println("start=" + startString);
			//System.out.println("stop=" + stopString);
			start = Integer.valueOf(startString);
			stop = Integer.valueOf(stopString);

		}
		
		if (start != 0 || stop != 0) {
			if (start > list.size())
				start = list.size();
			if (stop > list.size())
				stop = list.size();
			if (start < 1)
				start = 1;// if size is zero we need it to start at 1
			list = list.subList(start - 1, stop);
			// sources=paginatedSources;
		}
		return list;
	}

	public int getTotal() {
		// TODO Auto-generated method stub
		return total;
	}
	
}
